using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;
using Forum.Contracts;

namespace Forum.Tests.Application
{
    [TestFixture]
    public class testApplication
	{
        [Test]
        public void testLocal()
        {
            System.IO.File.Delete("test.vdb3");
            IDataAccess db = new Forum.DataAccess.DataAccessAdapter("test.vdb3");
            using (IApplication app = new Forum.Application.ApplicationLogic(db))
            {
                testForum(app);
            }
        }

        [Test]
        [Ignore]
        public void testRemote()
        {
            IDataAccess db = new Forum.DataAccess.DataAccessAdapter("", "");
            using (IApplication app = new Forum.Application.ApplicationLogic(db))
            {
                testForum(app);
            }
        }

        private void testForum(IApplication app)
        {
            Posting p;
            List<Posting> postings;

            postings = new List<Posting>(app.GetAllPostings());
            Assert.AreEqual(0, postings.Count);

            p = app.CreatePosting();
            p.Subject = "s1";
            p.Text = "t1";
            p.CreatedBy = "c1";
            app.StorePosting(p);
            postings.Add(p);

            p = app.CreatePosting();
            p.Subject = "s2";
            p.Text = "t2";
            p.CreatedBy = "c2";
            app.StorePosting(p);
            postings.Add(p);

            Assert.AreEqual(0, postings[0].Replies.Count);
            p = app.CreateReply(postings[0]);
            p.Subject = "s11";
            p.Text = "t11";
            p.CreatedBy = "c11";
            app.StorePosting(p);
            postings[0].Replies.Add(p);

            p = app.CreateReply(postings[0]);
            p.Subject = "s12";
            p.Text = "t12";
            p.CreatedBy = "c12";
            app.StorePosting(p);
            postings[0].Replies.Add(p);

            Assert.AreEqual(0, postings[1].Replies.Count);
            p = app.CreateReply(postings[1]);
            p.Subject = "s21";
            p.Text = "t21";
            p.CreatedBy = "c21";
            app.StorePosting(p);
            postings[1].Replies.Add(p);

            postings = new List<Posting>(app.GetAllPostings());
            Assert.AreEqual(2, postings.Count);
            Assert.AreEqual("s1", postings[0].Subject);
            Assert.AreEqual(2, postings[0].Replies.Count);
            Assert.AreEqual(1, postings[1].Replies.Count);

            app.Clear();
        }
	}

}
